package org.ricks.log;

import org.ricks.log.pattern.FormatterFactory;
import org.ricks.log.pattern.PatternFormatter;

import java.util.LinkedList;
import java.util.List;

/**
 * @author chenwei
 * @Description:
 * @date 2023/1/916:26
 */
public class RicksConfig {

    /**
     * 所有输出终端
     */
    private static final List<Appender> appenderList = createList();
    /**
     * 显示样式格式化
     */
    private static final List<PatternFormatter> formatterList = FormatterFactory.build(LogConfig.getFormat());


    /**
     * 处理日志信息.
     *
     * @param event 日志事件
     */
    public static void processLogEvent(LogEvent event) {
        // 1. 先构建输出结果
        final char[] text = event.build(formatterList);
        // 2. 投放所有要显示的终端
        appenderList.forEach(v -> v.output(event, text));
    }

    private static List<Appender> createList() {
        List<Appender> appenderList = new LinkedList<>();
        // 控制台输出
        if (LogConfig.isConsole()) {
            appenderList.add(new Appender.ConsoleAppender());
        }
        // 文件输出
        if (LogConfig.isPathInitialized()) {
            appenderList.add(new Appender.FileAppender());
        }
        return appenderList;
    }
}
